#include <iostream>
using namespace std;

/*
    虚继承:
        语法：class 子类：virtual public 父类{}
            virtual表示虚继承
        原理：
            1.子类虚继承父类，子类地址空间会出现一个指针
            2.该指针用来指向虚基类首地址的
            3。虚基类：Animal就是虚基类 （虚继承的父类就是虚基类）
        构建顺序：
            1.先构建Catamount类，Catamount类中有指针，通过该指针找到虚基类，然后构建Animal类，Catamount类
            2.再构建Mammal类，Mammal类中有指针，Mammal的指针会和Catamount类中的指针比较，发现相同则不再构建Animal类，直接构建Mammal类
                if(Catamount.指针 == Mammal.指针)  //相同则不再构建Animal类
            3.最后构建Leopard类

*/
class Animal
{
public:
    Animal()
    {
        cout << "Animal()" << endl;
    }
    void eat()
    {
        cout << "Animal吃" << endl;
    }
};
class Catamount : virtual public Animal
{
public:
    Catamount()
    {
        cout << "Catamount()" << endl;
    }
    void catchMouse()
    {
        cout << "Catamount抓老鼠" << endl;
    }
};
class Mammal : virtual public Animal
{
public:
    Mammal()
    {
        cout << "Mammal()" << endl;
    }
    void drinkMilk()
    {
        cout << "Mammal喝奶" << endl;
    }
};
class Leopard : public Catamount, public Mammal
{
public:
    Leopard()
    {
        cout << "Leopard()" << endl;
    }
    void play()
    {
        cout << "Leopard玩" << endl;
    }
};

int main()
{
    cout << "构建顺序：" << endl;
    Leopard lp;
    lp.eat();
    cout << "类的大小" << endl;
    cout << "Animal的大小:" << sizeof(Animal) << endl;
    cout << "Catamount的大小:" << sizeof(Catamount) << endl;
    cout << "Mammal的大小:" << sizeof(Mammal) << endl;
    cout << "Leopard的大小:" << sizeof(Leopard) << endl;

    return 0;
}